/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018-12-13 Time: 22:23
 *
 * 我的工单
 */

import React, {Component} from 'react';
import {
    SafeAreaView,
    View,
    Text,
    Image,
    TouchableOpacity, TouchableHighlight,
    RefreshControl,
    ScrollView,
    Modal, StyleSheet, Platform, BackHandler, ToastAndroid,NetInfo
} from 'react-native';
import {connect} from 'react-redux';
import OffWorkView from "../component/OffWorkView/OffWorkView";
import User from "../model/User/User";
import global from "../configuration/global";
import Sound from 'react-native-sound';
import {whskMenuMain} from "../screen/login/LoginScreen/Actions";
import {orderQmsRepairGetinfo} from "../component/JPushModalView/Action";
import JPushModal from "../component/JPushModalView/JPushModalView";
import {QmsRecordSummary} from "../screen/job/JobScreen/Actions";
import {AppRequestPath, ContextPath} from "../configuration/AppConfig";

class Homepage_repair extends Component {

    static navigationOptions = ({navigation}) => ({
        headerTitle: navigation.getParam('username'),
        headerLeft: <View/>,
        headerRight: <OffWorkView navigation={navigation}/>
    });

    // 组件渲染后调用
    componentDidMount() {
        // this.props.navigation.navigate('MainNavigator');
        this.props.navigation.navigate('MainNavigator',{transition:'forInitial'});
    }

    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            JPushModalVisiable:false,
            WebSocketData:[],
            userid:'',
            orderType:[],
            random:1,
        };

        User.getUser(user => {
            this.props.navigation.setParams({username:user.username+'-'+user.userid});
        });

        global.AppType = '工程';

        setInterval(() => {

            let WebSocketData = global.WebSocketData.data;
            let WebSocketData_new = [];
            let orderType = [];
            let random = global.WebSocketData.random;

            let {dispatch} = this.props;
            User.getUser(user => {
                if (user&&user.token) {
                    WebSocketData.map((item, index) => {
                        if(item.userid.toLowerCase()===user.userid.toLowerCase()  && item.jpush_alias===""){
                            WebSocketData_new.push(item)
                            orderType.push(item.ordertype)
                        }
                    })

                    if(global.WebSocketData.random !== global.WebSocketData.newrandom && JSON.stringify(WebSocketData_new)!== '[]' && global.AppType==="工程"){
                        console.log("=============播报语音初始化=============")
                        let demoAudio =  ContextPath()+"/attach/mobileBell.mp3";//支持众多格式
                        let s = new Sound(demoAudio, (e) => {
                            if (e) {
                                console.log('播放失败');
                                return
                            }
                            console.log("=============播报语音提醒=============")
                            s.play(() => s.release());
                        });
                        // s.play(() => s.release());
                        // s.play();
                        // s.play(() => s.release());
                        global.WebSocketData.newrandom = random;

                        // dispatch(whskMenuMain())
                        let str = {};
                        dispatch(QmsRecordSummary(JSON.stringify(str)))

                        this.setState({
                            WebSocketData: WebSocketData_new,
                            JPushModalVisiable:true,
                            userid:user.userid,
                            orderType:orderType,
                        })

                    }
                }
            })
        }, 1000)
    }

    render() {
        return (
            <SafeAreaView>
                <JPushModal JPushModalVisiable={this.state.JPushModalVisiable} WebSocketData={this.state.WebSocketData}
                            CloseJPushModal={()=>{
                                this.onCloseJPushModal()
                                // s.stop();
                            }}
                            SubmitJPushModal={()=>{
                                this.onCloseJPushModal()

                                this.props.navigation.navigate('MainNavigator',{"random":Math.random().toString()});
                                this.props.navigation.navigate('ImmediateOrderScreen',{"random":Math.random().toString()});
                                // s.stop();
                                // if(this.state.orderType.indexOf('清洁检查')>-1){
                                //     this.props.navigation.navigate('Homepage_repair',{"random":Math.random().toString()});
                                //     this.props.navigation.navigate('orderScreen',{"random":Math.random().toString()});
                                // }else if(this.state.orderType.indexOf('清洁')>-1){
                                //     this.props.navigation.navigate('Homepage_repair',{"random":Math.random().toString()});
                                //     this.props.navigation.navigate('orderScreen',{"random":Math.random().toString()});
                                // }else{
                                //     this.props.navigation.navigate('Homepage_repair',{"random":Math.random().toString()});
                                //     this.props.navigation.navigate('orderScreen',{"random":Math.random().toString()});
                                // }
                                let {dispatch} = this.props;
                                let orderpids_arr = [];
                                let orderpids = '';
                                let userid = '';
                                this.state.WebSocketData.map((item, index) => {
                                    orderpids_arr.push(...item.orderpids.split(","));
                                    userid = item.userid;
                                })
                                orderpids = Array.from(new Set(orderpids_arr)).join(',');
                                let str = {"arg":{"orderpids":orderpids,"userid":userid}};
                                dispatch(orderQmsRepairGetinfo(str));

                            }}
                />
            </SafeAreaView>
        )
    }

    onCloseJPushModal(){
        this.setState({
            JPushModalVisiable:false
        })
    }

}

const props = state => {
    return {

    }
};

export default Homepage_repair = connect(props)(Homepage_repair);